/**
 * @fileOverview The "contextmenu" plugin.
 */
VED.plugins.add( 'contextmenu', {
   requires: 'menu',
   lang: 'en,ru', // %REMOVE_LINE_CORE%

   // Make sure the base class (VED.menu) is loaded before it (#3318).
   onLoad: function() {
      VED.plugins.contextMenu = VED.menu.extend({

         initialize: function( editor ) {
            this.parent.call( this, editor, {
               panel: {
                  className: 'ved_menu_panel',
                  attributes: {
                     'aria-label': editor.lang.contextmenu.options
                  }
               }
            });
         },

         addTarget: function( element, nativeContextMenuOnCtrl ) {
            // Opera doesn't support 'contextmenu' event, we have duo approaches employed here:
            // 1. Inherit the 'button override' hack we introduced in v2 (#4530), while this require the Opera browser
            //  option 'Allow script to detect context menu/right click events' to be always turned on.
            // 2. Considering the fact that ctrl/meta key is not been occupied
            //  for multiple range selecting (like Gecko), we use this key
            //  combination as a fallback for triggering context-menu. (#4530)
            if ( VED.env.opera && !( 'oncontextmenu' in document.body ) ) {
               var contextMenuOverrideButton;
               element.on( 'mousedown', function( evt ) {
                  evt = evt.data;
                  if ( evt.$.button != 2 ) {
                     if ( evt.getKeystroke() == VED.CTRL + 1 )
                        element.fire( 'contextmenu', evt );
                     return;
                  }

                  if ( nativeContextMenuOnCtrl && ( VED.env.mac ? evt.$.metaKey : evt.$.ctrlKey ) )
                     return;

                  var target = evt.getTarget();

                  if ( !contextMenuOverrideButton ) {
                     var ownerDoc = target.getDocument();
                     contextMenuOverrideButton = ownerDoc.createElement( 'input' );
                     contextMenuOverrideButton.$.type = 'button';
                     ownerDoc.getBody().append( contextMenuOverrideButton );
                  }

                  contextMenuOverrideButton.setAttribute( 'style', 'position:absolute;top:' + ( evt.$.clientY - 2 ) +
                     'px;left:' + ( evt.$.clientX - 2 ) +
                     'px;width:5px;height:5px;opacity:0.01' );

               });

               element.on( 'mouseup', function( evt ) {
                  if ( contextMenuOverrideButton ) {
                     contextMenuOverrideButton.remove();
                     contextMenuOverrideButton = undefined;
                     // Simulate 'contextmenu' event.
                     element.fire( 'contextmenu', evt.data );
                  }
               });
            }

            element.on( 'contextmenu', function( event ) {
               var domEvent = event.data;

               if ( nativeContextMenuOnCtrl &&
               // Safari on Windows always show 'ctrlKey' as true in 'contextmenu' event,
               // which make this property unreliable. (#4826)
               ( VED.env.webkit ? holdCtrlKey : ( VED.env.mac ? domEvent.$.metaKey : domEvent.$.ctrlKey ) ) )
                  return;


               // Cancel the browser context menu.
               domEvent.preventDefault();

               var doc = domEvent.getTarget().getDocument(),
                  offsetParent = domEvent.getTarget().getDocument().getDocumentElement(),
                  fromFrame = !doc.equals( VED.document ),
                  scroll = doc.getWindow().getScrollPosition(),
                  offsetX = fromFrame ? domEvent.$.clientX : domEvent.$.pageX || scroll.x + domEvent.$.clientX,
                  offsetY = fromFrame ? domEvent.$.clientY : domEvent.$.pageY || scroll.y + domEvent.$.clientY;

               VED.tools.setTimeout( function() {
                  this.open( offsetParent, null, offsetX, offsetY );

                  // IE needs a short while to allow selection change before opening menu. (#7908)
               }, VED.env.ie ? 200 : 0, this );
            }, this );

            if ( VED.env.opera ) {
               // 'contextmenu' event triggered by Windows menu key is unpreventable,
               // cancel the key event itself. (#6534)
               element.on( 'keypress', function( evt ) {
                  var domEvent = evt.data;

                  if ( domEvent.$.keyCode === 0 )
                     domEvent.preventDefault();
               });
            }

            if ( VED.env.webkit ) {
               var holdCtrlKey,
                  onKeyDown = function( event ) {
                     holdCtrlKey = VED.env.mac ? event.data.$.metaKey : event.data.$.ctrlKey;
                  },
                  resetOnKeyUp = function() {
                     holdCtrlKey = 0;
                  };

               element.on( 'keydown', onKeyDown );
               element.on( 'keyup', resetOnKeyUp );
               element.on( 'contextmenu', resetOnKeyUp );
            }
         },

         open: function( offsetParent, corner, offsetX, offsetY ) {
            this.editor.focus();
            offsetParent = offsetParent || VED.document.getDocumentElement();

            // #9362: Force selection check to update commands' states in the new context.
            this.editor.selectionChange( 1 );

            this.show( offsetParent, corner, offsetX, offsetY );
         }
      });
   },

   beforeInit: function( editor ) {
      var contextMenu = editor.contextMenu = new VED.plugins.contextMenu( editor );

      editor.on( 'contentDom', function() {
         contextMenu.addTarget( editor.editable(), editor.config.browserContextMenuOnCtrl !== false );
      });

      editor.addCommand( 'contextMenu', {
         exec: function() {
            editor.contextMenu.open( editor.document.getBody() );
         }
      });

      editor.setKeystroke( VED.SHIFT + 121 /*F10*/, 'contextMenu' );
      editor.setKeystroke( VED.CTRL + VED.SHIFT + 121 /*F10*/, 'contextMenu' );
   }
});






